package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICompanySaleRebatesRulesDAO;
import org.jsola.hr.dto.CompanySaleRebatesRulesAddDTO;
import org.jsola.hr.dto.CompanySaleRebatesRulesUpdateDTO;
import org.jsola.hr.entity.CompanySaleRebatesRulesDO;
import org.jsola.hr.query.CompanySaleRebatesRulesQuery;
import org.jsola.hr.service.ICompanySaleRebatesRulesService;
import org.jsola.hr.vo.CompanySaleRebatesRulesListVO;
import org.jsola.hr.vo.CompanySaleRebatesRulesVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用工单位返费关联表
 *
 * @author zhr
 */
@Service("hrCompanySaleRebatesRulesServiceImpl")
public class CompanySaleRebatesRulesServiceImpl implements ICompanySaleRebatesRulesService {

    @Autowired
    private ICompanySaleRebatesRulesDAO companySaleRebatesRulesDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CompanySaleRebatesRulesVO save(CompanySaleRebatesRulesAddDTO companySaleRebatesRulesAddDTO, TokenUser tokenUser) {
        // 转DO
        CompanySaleRebatesRulesDO companySaleRebatesRulesDO = companySaleRebatesRulesAddDTO.to(CompanySaleRebatesRulesDO.class);
        // 保存
        companySaleRebatesRulesDO = save(companySaleRebatesRulesDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return companySaleRebatesRulesDO.to(CompanySaleRebatesRulesVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CompanySaleRebatesRulesUpdateDTO companySaleRebatesRulesUpdateDTO, TokenUser tokenUser) {
        // 这里不做更新操作,把他以前的关联关系删除掉,新增一条新的关联关系，这样好追溯问题
        int result = this.deleteByIds(companySaleRebatesRulesUpdateDTO.getId());
        CompanySaleRebatesRulesAddDTO add = companySaleRebatesRulesUpdateDTO.to(CompanySaleRebatesRulesAddDTO.class);
        this.save(add, tokenUser);
        return result;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... companySaleRebatesRulesIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) companySaleRebatesRulesIds);
    }


    @Override
    public CompanySaleRebatesRulesVO selectById(String companySaleRebatesRulesId, String siteId) {
        CompanySaleRebatesRulesDO companySaleRebatesRulesDO = selectDOById(companySaleRebatesRulesId, siteId);
        if (companySaleRebatesRulesDO == null) {
            return null;
        }
        return companySaleRebatesRulesDO.to(CompanySaleRebatesRulesVO.class);
    }

    @Override
    public List<CompanySaleRebatesRulesListVO> select(CompanySaleRebatesRulesQuery companySaleRebatesRulesQuery, String siteId) {
        List<CompanySaleRebatesRulesDO> companySaleRebatesRulesDOList = selectDO(companySaleRebatesRulesQuery, siteId);
        if (CollectionUtils.isEmpty(companySaleRebatesRulesDOList)) {
            return companySaleRebatesRulesDOList == null ? null : new ArrayList<>();
        }
        return companySaleRebatesRulesDOList.stream()
                .map(companySaleRebatesRulesDO -> companySaleRebatesRulesDO.to(CompanySaleRebatesRulesListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CompanySaleRebatesRulesQuery companySaleRebatesRulesQuery, String siteId) {
        Example example = buildExample(companySaleRebatesRulesQuery, siteId);
        return companySaleRebatesRulesDAO.selectCountByExample(example);
    }

    @Override
    public Page<CompanySaleRebatesRulesListVO> selectPage(CompanySaleRebatesRulesQuery companySaleRebatesRulesQuery, String siteId) {
        Example example = buildExample(companySaleRebatesRulesQuery, siteId);
        Page<CompanySaleRebatesRulesDO> page = companySaleRebatesRulesDAO.selectPageByExample(example,
                companySaleRebatesRulesQuery.getPageNo(),
                companySaleRebatesRulesQuery.getPageSize());

        return page.to(CompanySaleRebatesRulesListVO.class);
    }

    @Override
    public CompanySaleRebatesRulesDO selectDOById(String companySaleRebatesRulesId, String siteId) {
        return listById(companySaleRebatesRulesId, siteId);
    }

    @Override
    public List<CompanySaleRebatesRulesDO> selectDO(CompanySaleRebatesRulesQuery companySaleRebatesRulesQuery, String siteId) {
        Example example = buildExample(companySaleRebatesRulesQuery, siteId);
        return companySaleRebatesRulesDAO.selectByExample(example);
    }

    @Override
    public CompanySaleRebatesRulesDO selectRulesByCompanyId(String companyId, String siteId) {
        Example example = new Example(CompanySaleRebatesRulesDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("companyId", companyId)
                .andEqualTo("siteId", siteId);
        return companySaleRebatesRulesDAO.selectOneByExample(example);
    }

    @Override
    public void deleteByRulesIdList(List<String> rulesIdList) {
        CompanySaleRebatesRulesDO template = new CompanySaleRebatesRulesDO();
        template.setValid(false);
        Example example = new Example(CompanySaleRebatesRulesDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andIn("rebatesRulesId", rulesIdList);
        companySaleRebatesRulesDAO.updateByExampleSelective(template,example);
    }

    @Override
    public List<CompanySaleRebatesRulesDO> selectByRulesId(String rulesId, TokenUser tokenUser) {
        Example example = new Example(CompanySaleRebatesRulesDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("rebatesRulesId", rulesId)
                .andEqualTo("siteId", tokenUser.getSiteId());
        return companySaleRebatesRulesDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param companySaleRebatesRulesQuery 查询参数
     * @param siteId                       所属站点id
     * @return example
     */
    private Example buildExample(CompanySaleRebatesRulesQuery companySaleRebatesRulesQuery, String siteId) {
        Example example = new Example(CompanySaleRebatesRulesDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, companySaleRebatesRulesQuery.getOrders());
        return example;
    }
}




